clear all 
set more off
*set maxvar 20000
*set matsize 800
cd "C:\Users\Lester\Dropbox\ June 2016\Writeup\Economic Inquiry\EI R and R"
use "C:\Users\Lester\Dropbox\\data_dta\master.dta", clear

tostring id grade year, replace
gen course = grade + " " + subject + " " + cohort
gen class = year + " " + course
gen studentxclass = id + " " + class
gen gradebook = year + grade + cohort
destring id grade year, replace

drop if substr(subject, 1, 2) == "it" | substr(subject, 1, 2) == "cs" 
	
global controls order_ass num_month bulg

gen malelate = male*late_start
gen maleteachermale = male*teacher_male


*****************************************************************   
/*						PANEL A	  - Middle Schoolers	   */	   
*****************************************************************
eststo clear

preserve
keep if hs==0

eststo clear

eststo: xi: qui reg Zmarks i.male, vce(cluster class)
eststo: xi: qui reg Zmarks i.male malelate maleteachermale teacher_male late_start, vce(cluster class)
eststo: xi: qui reg Zmarks i.male malelate maleteachermale teacher_male late_start $controls, vce(cluster class)
eststo: xi: qui areg Zmarks i.male malelate maleteachermale teacher_male late_start, absorb(class) vce(cluster class)
eststo: xi: qui areg Zmarks i.male malelate maleteachermale teacher_male late_start $controls, absorb(class) vce(cluster class)


label var _Imale "\ \ \ Male"
label var maleteachermale "\ \ \ Male X \\ \hspace{0.3cm} Male Teacher"
label var malelate "\ \ \ Male X \\ \hspace{0.3cm} Late Start"

esttab using "results-age.tex", ///
    keep(_Imale_1 maleteachermale malelate) replace se(3) b(3) r2 star(* 0.10 ** 0.05 *** 0.01) nonum nonotes ///
	refcat(_Imale_1 "\underline{Middle schoolers}", nolabel) noconstant nogaps ///
    mtitles("(1)" "(2)" "(3)" "(4)" "(5)") label postfoot("")

restore

*****************************************************************   
/*						PANEL B - High Schoolers			   */	   
*****************************************************************
preserve
keep if hs==1

eststo clear

eststo: xi: qui reg Zmarks male, vce(cluster class)
estadd local classfe " "
*estadd local studentfe " "
estadd local monthfe " "
estadd local controls " "  
eststo: xi: qui reg Zmarks male i.male*late_start i.male*i.teacher_male, vce(cluster class)
estadd local classfe " "
*estadd local studentfe " "
estadd local monthfe " "
estadd local controls " "   
eststo: xi: qui reg Zmarks male i.male*late_start i.male*i.teacher_male $controls, vce(cluster class)
estadd local classfe " "
*estadd local studentfe " "
estadd local monthfe " "
estadd local controls "X"
eststo: xi: qui areg Zmarks male i.male*late_start i.male*i.teacher_male, absorb(class) vce(cluster class)
estadd local classfe "X"
*estadd local studentfe " "
estadd local monthfe " "
estadd local controls " "
eststo: xi: qui areg Zmarks male i.male*late_start i.male*i.teacher_male $controls, absorb(class) vce(cluster class)
estadd local classfe "X"
*estadd local studentfe " "
estadd local monthfe " "
estadd local controls "X"

label var male "\ \ \ Male"
label var _ImalXtea "\ \ \ Male X \\ \hspace{0.3cm} Male Teacher"
label var _ImalXlate "\ \ \ Male X \\ \hspace{0.3cm} Late Start"

esttab using "results-age.tex", ///
	keep( _ImalXtea_1_1 _ImalXlate__1 male) append se(3) b(3) r2 star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant nogaps ///
	refcat(male "\hline \underline{High schoolers}", nolabel) ///
	scalars("classfe Class FE" "monthfe Month FE" "controls Controls") ///
    mtitles("" "" "" "" "" "" "" "") label prehead("") posthead("") 
* "studentfe Student FE"
restore
